﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace G.APITest.Utils
{
    public static class ExtendPageUtiles
    {
        private const string DirectoryName = "APPPlugin";
        private const string dbAddress = DirectoryName + "/APPPluginInfo.json";
        private static ObservableCollection<Data.APPPluginInfo> appPluginInfos;
        public static ObservableCollection<Data.APPPluginInfo> AppPluginInfos
        {
            get
            {
                if (appPluginInfos == null)
                {
                    using (var isoFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isoFile.DirectoryExists(DirectoryName))
                            isoFile.CreateDirectory(DirectoryName);
                    }
                    appPluginInfos = G.SL.Utils.APPFileHelp.DeserializeObjectFromFile<ObservableCollection<Data.APPPluginInfo>>(dbAddress, G.SL.Utils.FileContentType.JSON);
                    if (appPluginInfos == null)
                    {
                        appPluginInfos = new ObservableCollection<Data.APPPluginInfo>();
                    }
                }
                return appPluginInfos;
            }
        }

        private static void SaveAppPluginInfos()
        {
            if (AppPluginInfos != null)
                G.SL.Utils.APPFileHelp.SerializeObjectToFile(dbAddress, AppPluginInfos, G.SL.Utils.FileContentType.JSON);
        }

        public static void DeletePlugin(G.APITest.Data.APPPluginInfo a)
        {
            if (MessageBox.Show("确定要删除？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (a != null && AppPluginInfos.Contains(a))
                {
                    using (var isofile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (isofile.FileExists(a.ISOPath))
                            isofile.DeleteFile(a.ISOPath);
                    }
                    AppPluginInfos.Remove(a);
                    SaveAppPluginInfos();
                }
            }
        }

        public static void AddPlugin(byte[] buffer, string url, string pluginName)
        {
            string name = System.IO.Path.GetFileNameWithoutExtension(url);
            var appPluginInfo = AppPluginInfos.FirstOrDefault(c => c.Name == pluginName);
            //保存文件
            using (var isofile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var dllFile = isofile.CreateFile(DirectoryName + "/" + name))
                {
                    dllFile.Write(buffer, 0, buffer.Length);
                    dllFile.Close();
                }
                if (appPluginInfo == null)
                {
                    AppPluginInfos.Add(new Data.APPPluginInfo() { Name = pluginName, ISOPath = DirectoryName + "/" + name, DLLName = name, Uri = url });
                    MessageBox.Show("安装完毕！请重启程序！");
                }
                else
                {
                    appPluginInfo.ISOPath = DirectoryName + "/" + name;
                    appPluginInfo.DLLName = name;
                    appPluginInfo.Uri = url;
                    MessageBox.Show("更新完毕！请重启程序！");
                }
                SaveAppPluginInfos();

            }
        }
    }
}
